$(document).ready(function() {

    /* load */
    
    var myOptions = {
        zoom: 9,
        center: new google.maps.LatLng(40.81940575,-73.95647955),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    
    var map = new google.maps.Map(document.getElementById("map"), myOptions);

/*
    if (GBrowserIsCompatible()) {
        var markers = [];

        var map = new GMap2(document.getElementById('map')), geocoder = new GClientGeocoder();
        map.enableScrollWheelZoom();    

        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.addControl(new GOverviewMapControl());
        
        map.setCenter(new GLatLng(49.00, 16.00), 16);
    
		// create marker manager
		mgr = new MarkerManager(map, {trackMarkers: true});
    } else {
        alert("Sorry, the Google Maps API is not compatible with this browser.");
    }
    */

    // create marker
    function createMarker(point, title, description, course, id) {
        var marker = new GMarker(point, {title: title, draggable: false});
            	
        marker.id    = id;
        marker.title = title;
        
        markers.push(marker);
        
        GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(description);
        });
    
        mgr.addMarker(marker, 9, 17);
    
        return marker;
    }
    
/*

    // markers of selected courses
    function searchStudentsNear(center) {
        var courses = [];
        
        $.get('/ajax/get_markers.php', {lat: center.lat(), lng: center.lng(), 2, courses: courses.join(',')}, function(xml) {
            var m = xml.documentElement.getElementsByTagName('marker');

            if (m.length) {
                markers = [];
                mgr.clearMarkers();
            	map.clearOverlays();

                var bounds = new GLatLngBounds();
                
                $.each(m, function() {
                    var point = new GLatLng(parseFloat(this.getAttribute('lat')), parseFloat(this.getAttribute('lng')));
                    
                    createMarker(
                        point,
                        this.getAttribute('title'), 
                        this.getAttribute('html'), 
                        this.getAttribute('course'),
                        this.getAttribute('id')
                    );
                
                    bounds.extend(point);
                });
                
            	mgr.refresh();
                map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
            } else {
                alert('No results found.');
            }
        });
    }

    */

    // markers of only one course
    function showCourse(category) {
        $.get("/ajax/get_markers.php", {course: course}, function(xml) {
        	var m = xml.documentElement.getElementsByTagName("marker");
        	
            $.each(m, function() {
                createMarker(                                     
                    new GLatLng(parseFloat(this.getAttribute("lat")), parseFloat(this.getAttribute("lng"))),
                    this.getAttribute("title"), 
                    this.getAttribute("html"), 
                    this.getAttribute("course"),
                    this.getAttribute("id")
                );
            });
        	
        	mgr.refresh();
        });
    }
    
});
